﻿Imports System.Text
Public Class Convert_Encoding
    Private codepagelist As New ArrayList

    Sub New()
        If MainForm.conLanguageDe.Checked Then
            System.Threading.Thread.CurrentThread.CurrentUICulture = New Globalization.CultureInfo("de")
        Else
            System.Threading.Thread.CurrentThread.CurrentUICulture = New Globalization.CultureInfo("en")
        End If
        Me.KeyPreview = True
        InitializeComponent()
    End Sub

    Private Sub textcoding_Convert_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Location = New Point(MainForm.Location.X + 50, MainForm.Location.Y + 50)
        For Each codepage In Encoding.GetEncodings
            lstEncodings.Items.Add(codepage.DisplayName)
            codepagelist.Add(codepage.GetEncoding)
        Next
        If MainForm.textdoc.TextPath = "" Then radBitwise.Enabled = False
        lstEncodings.SelectedItem = MainForm.textdoc.TextEncoding.EncodingName
        Me.AcceptButton = butChange
    End Sub

    Private Sub key_Check(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.Close()
        End If
    End Sub

    Private Sub butCancel_Click(sender As Object, e As EventArgs) Handles butCancel.Click
        Me.Close()
    End Sub

    Private Sub butChange_Click(sender As Object, e As EventArgs) Handles butChange.Click
        Dim new_encoding As Encoding = codepagelist(lstEncodings.SelectedIndex)
        If radBitwise.Checked Then
            If MainForm.textdoc.ConvertEncoding(new_encoding, True) Then
                MainForm.statInfo.Text = My.Resources.saveInfo
                MainForm.txtMain.SelectionLength = 0
                MainForm.statEncoding.Text = MainForm.textdoc.TextEncoding.EncodingName
                Me.Close()
            End If
        Else
            MainForm.textdoc.ConvertEncoding(new_encoding, False)
            MainForm.txtMain.SelectionLength = 0
            MainForm.statEncoding.Text = MainForm.textdoc.TextEncoding.EncodingName
            Me.Close()
        End If
    End Sub
End Class